<?php
/** @noinspection PhpUnused */
declare(strict_types=1);

namespace app\shop\controller;


use app\shop\exception\FailException;
use app\shop\exception\ParamsErrorException;
use app\shop\library\ShopAuth;
use app\shop\library\ShopLogHelper;
use app\shop\logic\OrderLogic;
use app\shop\validate\OrderValidate;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\response\Json;
use Throwable;

/**
 * @property OrderLogic $logic
 */
class OrderController extends BaseController
{
    protected string $validateClassName = OrderValidate::class;

    public function __construct(OrderLogic $orderLogic)
    {
        $this->logic = $orderLogic;

        parent::__construct();
    }

    protected array $noNeedLogin = [];

    /**
     * 下单前(直接下单)
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function pre(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'pre');

        $result = $this->logic->getPreByNow($params);

        return $this->success($result);
    }

    /**
     * 下单前(购物车下单)
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws ParamsErrorException|FailException
     */
    public function preCart(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'pre_cart');

        $result = $this->logic->getPreByCart($params);

        return $this->success($result);
    }

    /**
     * 下单前(再次购买下单)
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws FailException
     * @throws ModelNotFoundException
     * @throws ParamsErrorException
     */
    public function preRepurchase(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'pre_purchase');

        $result = $this->logic->getPreByRepurchase($params);

        return $this->success($result);
    }

    /**
     * 下单操作
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException|Throwable
     */
    public function create(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'create');

        $result = $this->logic->create($params);

        return $this->success($result);
    }

    /**
     * 支付
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function pay(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'pay');

        $result = $this->logic->pay($params);

        return $this->success($result);
    }

    /**
     * 支付后主动查询
     * @return Json
     * @throws ParamsErrorException
     */
    public function payNotify(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'pay_notify');

        try {
            $this->logic->processNotify($params['id']);
        } catch (Throwable $e) {
            return $this->fail($e->getMessage() ?: '支付状态确认中,请返回列表刷新', ['err' => $e->getMessage()]);
        }

        return $this->success('支付成功');
    }

    /**
     * 订单列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function list(): Json
    {
        $params = $this->getInput(true);
        $user = ShopAuth::instance()->getUser();
        $params['user_id'] = $user->id;
        $this->validateData($params, 'list');

        $result = $this->logic->list($params);
        return $this->success($result);
    }

    /**
     * 订单列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function storeorderlist(): Json
    {
        $params = $this->getInput(true);
        $user = ShopAuth::instance()->getUser();
        $params['user_id'] = $user->id;
        $result = $this->logic->storeorderlist($params);
        return $this->success($result);
    }

    /**
     * 订单详情
     * @return Json
     * @throws ParamsErrorException
     */
    public function storeorderdetail(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'detail');

        $result = $this->logic->storeorderdetail($params);

        return $this->success($result);
    }


    /**
     * 抢单列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function Playlist(): Json
    {
        $params = $this->getInput(true);
        $params['status'] = 2;

        $this->validateData($params, 'list');

        $result = $this->logic->list($params);

        return $this->success($result);
    }

    public function surePlayOrder()
    {
        $params = $this->postInput(true);
        $this->validateData($params, 'toRobberyOrder');
        $result = $this->logic->surePlayOrder($params);
        return $this->success($result);

    }

    /**
     * 陪玩接待列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function userPlaylist(): Json
    {
        $params = $this->getInput(true);
        $this->validateData($params, 'list');
        $user = ShopAuth::instance()->getUser();
        $params['user_id'] = $user->id;
        $result = $this->logic->userPlaylist($params);

        return $this->success($result);
    }

    /**
     *
     * @return \Psr\Http\Message\StreamInterface|Json
     * @throws ParamsErrorException
     */
    public function toRobberyOrder()
    {
        $params = $this->postInput(true);
        $this->validateData($params, 'toRobberyOrder');
        $result = $this->logic->toRobberyOrder($params);
        return $this->success($result);
    }

    /**
     * 订单详情
     * @return Json
     * @throws ParamsErrorException
     */
    public function detail(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'detail');

        $result = $this->logic->detail($params);

        return $this->success($result);
    }

    /**
     * 取消订单
     * @return Json
     * @throws ParamsErrorException
     */
    public function cancel(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'cancel');

        try {
            $result = $this->logic->cancel($params);
        } catch (Exception $e) {
            ShopLogHelper::error('订单取消失败', $params, $e);
            $result = false;
        }

        return $this->success($result);
    }

    /**
     * 删除订单
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function delete(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'delete');

        $this->logic->delete($params);

        return $this->success('删除成功');
    }

    /**
     * 订单收货
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function receive(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'receive');

        $result = $this->logic->receive($params);

        return $this->success($result);
    }

    /**
     * 订单物流轨迹
     * @return Json
     * @throws ParamsErrorException
     * @throws Exception
     */
    public function delivery(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'delivery');

        $result = $this->logic->getDeliveryTracks($params['id']);

        return $this->success($result);
    }
}
